home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2719 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  55 lines

  1. Path: EU.net!sun4nl!xs4all!usenet
  2. From: Gertjan Klein <gklein@xs4all.nl>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help ?!
  5. Date: 22 Jan 1996 11:15:16 GMT
  6. Organization: XS4ALL, networking for the masses
  7. Message-ID: <4dvrk4$f2d@news.xs4all.nl>
  8. References: <4dm889$3hs@neptunus.pi.net> <4drnv1$cr@news.iag.net>
  9. NNTP-Posting-Host: xs1.xs4all.nl
  10.  
  11. In article <4drnv1$cr@news.iag.net>, jatmon@iag.net (John R Buchan) wrote:
  12.  
  13. [Correction of someone else's code, mostly snipped]
  14.  
  15.   I have a question about this specific fragment of code, in particular
  16. the last line of it.
  17.  
  18.  >  do
  19.  >    {
  20.  >     *cpy++ = *cmd++;
  21.  >    }
  22.  >  while (!strchr(" /-\0", *cmd));
  23.  
  24.   Why do you include a '\0' in the string? (If this was the original
  25. poster's code, the question changes to: Why did you leave it in?) It
  26. doesn't make much sense to me, as '\0' is the string terminator, and it
  27. is already there. I.e., the following code:
  28.  
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. int main(void) {
  33.  
  34.   int c = '\0';
  35.  
  36.   printf("Character %sfound in string.\n",
  37.           strchr(" /-", c) ? "" : "not ");
  38.   return 0;
  39. }
  40.  
  41.   ... displays "Character found in string." whether I include the '\0'
  42. or not. The basic question is, is the terminating zero regarded part of
  43. the string here? My implementation's strchr() seems to imply it is, but
  44. I'd like to know if this always the case. I couldn't find anything in
  45. K&R2 about how strchr() behaves when the int to search for is 0. What's
  46. to keep it from first checking for the string terminator before doing
  47. the compare? (In either case, including the '\0' in the string wouldn't
  48. make a difference, would it?)
  49.  
  50.   Gertjan.
  51.  
  52. ---
  53. -- 
  54. Gertjan Klein   <gklein@xs4all.nl>
  55.